<?php

require_once( 'HTTPRequest.class.php' );

class RESTClient {
    private $user = null;
    private $pass = null;
    private $response = null;
    private $request = null;

    public function __construct( $_user, $_pass ) {
      $this->user = $_user;
      $this->pass = $_pass;
    }
    
    public function newRequest( $_url, $_data = null, $_method = 'POST', $_contentType ) {
        $this->request = new HTTPRequest( $_url, $_data, $_method, $_contentType );
        
        $this->addHeader( 'Authorization', 'Basic '.base64_encode( $this->user.':'.$this->pass ) );
        
        $this->response = $this->request->process();
        
        return $this->response;
    }
    
    public function addHeader( $_param, $_value ) {
      if( $this->request == null )
        throw new Exception( 'Cannot add header without calling newRequest first' );
        
      $this->request->addHeader( $_param, $_value );
    }
    
    public function getResponse()
    {
      if( $this->request == null )
        throw new Exception( 'Cannot getResponse without calling newRequest first' );
        
      return $this->request->getResponse();
    }
    
    public function getResponseCode()
    {
      if( $this->request == null )
        throw new Exception( 'Cannot getResponseCode without calling newRequest first' );
        
      return $this->request->getResponseCode();
    }
}
?>